﻿ 

using System.Collections.Generic;
using System;
using BCService.Utils;
using BCCommon;
using BCData.TMS.VehicleTransportFaq;
using BCDto.TMS.VehicleTransportFaq;
using BCEntity.TMS.VehicleTransportFaq;
using BCDto.Sim.Admin.Admin;
using BCEntity.Common.EntityNameConst;
using System.Linq;

namespace BCService.TMS.VehicleTransportFaq
{
    public class VehicleTransportFaqService : IVehicleTransportFaqService
    {
        #region var
        private readonly IDatabaseContext databaseContext;
        private IVehicleTransportFaqData vehicleTransportFaqData;

        private readonly IRedisService redisService;

        private readonly string key = string.Format("{0}", EntityNameConst.VehicleTransportFaqEntity);

        #endregion

        #region 构造函数
        public VehicleTransportFaqService(IRedisService redisService,IVehicleTransportFaqData vehicleTransportFaqData, IDatabaseContext databaseContext)
        {
            this.redisService = redisService;
            this.vehicleTransportFaqData = vehicleTransportFaqData;
            this.databaseContext = databaseContext;
        }
        #endregion

        #region Add
        /// <summary>
        /// 添加新车辆运输常见问题纪录
        /// </summary>
        /// <param name="vehicleTransportFaqRequestDto"></param>
        /// <param name="admin"></param>
        /// <returns></returns>
        public VehicleTransportFaqDto Add(VehicleTransportFaqRequestDto vehicleTransportFaqRequestDto, AdminDto admin)
        {
            VehicleTransportFaqEntity entity = vehicleTransportFaqRequestDto.As<VehicleTransportFaqEntity>();
            entity.IsDelete = false;
            entity.Enabled = true;
            entity.IsReply = false;
            entity.IsSolve = false;
            entity.ReplyEmail = "Disable";
            entity.CreateTime = DateTime.Now;
            return vehicleTransportFaqData.InsertAsync(entity).GetAwaiter().GetResult().As<VehicleTransportFaqDto>();
        }
        #endregion

        #region update
        public VehicleTransportFaqDto Update(VehicleTransportFaqDto vehicleTransportFaqDto, AdminDto admin)
        {
            var org = vehicleTransportFaqData.GetEntityAsync(vehicleTransportFaqDto.VehicleTransportFaqId).GetAwaiter().GetResult() ?? throw new ArgumentException("找不到修改记录!");

            var entity = vehicleTransportFaqDto.As<VehicleTransportFaqEntity>();
            entity.CreateTime = org.CreateTime;

            this.redisService.DeleteMultipleKey(key, false);
            return vehicleTransportFaqData.UpdateAsync(entity).GetAwaiter().GetResult().As<VehicleTransportFaqDto>();

        }
        #endregion
         

        #region 更新 回复信息
        public VehicleTransportFaqDto UpdateAnswer(long vehicleTransportFaqId, string answer, string keyWord, bool? isSolve, AdminDto admin)
        {
            var entity = vehicleTransportFaqData.GetEntityAsync(vehicleTransportFaqId).GetAwaiter().GetResult() ?? throw new ArgumentException("找不到修改记录!");
            entity.Answer = answer;
            if(isSolve.HasValue)
                entity.IsSolve = isSolve.Value;
            entity.IsReply = true;
            entity.ReplyTime = DateTime.Now;
            entity.AdminId = admin.AdminId;
            entity.AdminFullName = admin.FullName;

            this.redisService.DeleteMultipleKey(key, false);
            return vehicleTransportFaqData.UpdateAsync(entity).GetAwaiter().GetResult().As<VehicleTransportFaqDto>();
        }
        #endregion


        #region 修改 记录状态
        public VehicleTransportFaqDto UpdateStatus(long vehicleTransportFaqId, bool? isShow, bool? isRelay, bool? isSolve, bool? enabled, bool? isDelete)
        {
            var entity = vehicleTransportFaqData.GetEntityAsync(vehicleTransportFaqId).GetAwaiter().GetResult() ?? throw new ArgumentException("找不到修改记录!");
            if (isDelete.HasValue)
                entity.IsDelete = isDelete.Value;
            if (isShow.HasValue)
                entity.IsShow = isShow.Value;
            if (isRelay.HasValue)
                entity.IsReply = isRelay.Value;
            if (isSolve.HasValue)
                entity.IsSolve = isSolve.Value;
            if (enabled.HasValue)
                entity.Enabled = enabled.Value;
            this.redisService.DeleteMultipleKey(key, false);
            return vehicleTransportFaqData.UpdateAsync(entity).GetAwaiter().GetResult().As<VehicleTransportFaqDto>();
        }
        #endregion


        #region 修改 记录状态
        public VehicleTransportFaqDto UpdateStatus(VehicleTransportFaqStatusPutDto vehicleTransportFaqStatusPutDto)
        {
            return UpdateStatus(vehicleTransportFaqStatusPutDto.VehicleTransportFaqId, vehicleTransportFaqStatusPutDto.IsShow, vehicleTransportFaqStatusPutDto.IsReply, vehicleTransportFaqStatusPutDto.IsSolve, vehicleTransportFaqStatusPutDto.Enabled, vehicleTransportFaqStatusPutDto.IsDelete);
        }
        #endregion


        #region Delete
        public void Delete(long vehicleTransportFaqId)
        {  
            this.redisService.DeleteMultipleKey(key, false);
            vehicleTransportFaqData.DeleteAsync(vehicleTransportFaqId).GetAwaiter().GetResult();

        }
        #endregion

        #region GetByVehicleTransportFaqId
        public VehicleTransportFaqDto GetById(long vehicleTransportFaqId)
        {
            string cacheKey = this.redisService.GetKey(key, vehicleTransportFaqId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var a = vehicleTransportFaqData.GetEntityAsync(vehicleTransportFaqId).GetAwaiter().GetResult();
                return a.As<VehicleTransportFaqDto>();
            }
            , TimeSpan.FromDays(1));
            return result;
        }
        #endregion

        #region GetList  获取车辆运输常见问题记录--[分页]
        public PageableList<VehicleTransportFaqDto> GetList(int? faqCategoryId,string title,string question,string answer,string keyWord,bool? isShow , bool? isDelete,bool? isReply,bool? isSolve,bool? enable,DateTime? start,DateTime? end,string replyEmail, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, faqCategoryId, title, question, answer,  keyWord,  isShow ,  isDelete, isReply,  isSolve,  enable,  start,  end, replyEmail,  sortName,  ascending, pageIndex, pageSize);

            var result = this.redisService.TryGet(cacheKey, () =>
            {
                int fCount = 0;
                IEnumerable<VehicleTransportFaqEntity> fList;

                (fList, fCount) = vehicleTransportFaqData.GetListAsync(faqCategoryId, title, question, answer, keyWord, isShow, isDelete, isReply, isSolve, enable, start, end, replyEmail, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();

                return new PageableList<VehicleTransportFaqDto>()
                {
                    Count = fCount,
                    Items = fList.As<IEnumerable<VehicleTransportFaqDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now,
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion


        #region GetList  获取车辆运输常见问题记录
        public List<VehicleTransportFaqDto> GetList(int? faqCategoryId, string title, string question, string answer, string keyWord, bool? isShow, bool? isDelete, bool? isReply, bool? isSolve, bool? enable, DateTime? start, DateTime? end, string replyEmail, string sortName, bool? ascending)
        {
            string cacheKey = this.redisService.GetKey(key, faqCategoryId, title, question, answer, keyWord, isShow, isDelete, isReply, isSolve, enable, start, end, replyEmail, sortName, ascending);

            var result = this.redisService.TryGet(cacheKey, () =>
            {
                return vehicleTransportFaqData.GetListAsync(faqCategoryId, title, question, answer, keyWord, isShow, isDelete, isReply, isSolve, enable, start, end, replyEmail, sortName, ascending).GetAwaiter().GetResult().As<IEnumerable<VehicleTransportFaqDto>>().ToList();
                 
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion

    }
}